package com.changpei.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.ContentTmp;
import com.changpei.core.service.ContentTmpService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * <p>Title : ContentTmp</p>
 * <p>Description : contentTmp的控制层</p>
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/contentTmp")
@CrossOrigin
public class ContentTmpController {

    @Autowired
    private ContentTmpService contentTmpService;

    /**
     *
     * <p>Title : add</p>
     * <p>Description : 新增ContentTmp数据</p>
     * @param contentTmp
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Long> add(@RequestBody ContentTmp contentTmp) {
        // 调用ContentTmpService实现添加ContentTmp
        if (contentTmpService.save(contentTmp)) {
            return Result.ok(contentTmp.getId());
        }
        return Result.failed();
    }

    /**
     *
     * <p>Title : delete</p>
     * <p>Description : ContentTmp根据ID数据</p>
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用ContentTmpService实现根据主键删除
        if (contentTmpService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     *
     * <p>Title : update</p>
     * <p>Description : ContentTmp根据ID修改</p>
     * @param contentTmp
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody ContentTmp contentTmp, @PathVariable Long id) {
        // 设置主键值
        contentTmp.setId(id);
        // 调用ContentTmpService实现修改ContentTmp
        if (contentTmpService.updateById(contentTmp)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     *
     * <p>Title : findPage</p>
     * <p>Description : ContentTmp分页搜索实现</p>
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<ContentTmp>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentTmpService实现分页查询ContentTmp
        Page<ContentTmp> pageInfo = contentTmpService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     *
     * <p>Title : findPage</p>
     * <p>Description : ContentTmp分页条件搜索实现</p>
     * @param contentTmp
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<ContentTmp>> findPage(@RequestBody(required = false) ContentTmp contentTmp, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentTmpService实现分页条件查询ContentTmp
        Page<ContentTmp> pageInfo = contentTmpService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(contentTmp));
        return Result.ok(pageInfo);
    }

    /**
     *
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索ContentTmp数据</p>
     * @param contentTmp
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<ContentTmp>> findList(@RequestBody(required = false) ContentTmp contentTmp) {
        // 调用ContentTmpService实现条件查询ContentTmp
        List<ContentTmp> list = contentTmpService.list(SearchUtil.initQuery(contentTmp));
        return Result.ok(list);
    }

    /**
     *
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询ContentTmp数据</p>
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<ContentTmp> findById(@PathVariable Long id) {
        // 调用ContentTmpService实现根据主键查询ContentTmp
        ContentTmp contentTmp = contentTmpService.getById(id);
        return Result.ok(contentTmp);
    }

    /**
     *
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索ContentTmp数据，结果返回一条</p>
     * @param contentTmp
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<ContentTmp> findOne(@RequestBody(required = false) ContentTmp contentTmp) {
        // 调用ContentTmpService实现条件查询ContentTmp
        ContentTmp contentTmpResult = contentTmpService.getOne(SearchUtil.initQuery(contentTmp));
        return Result.ok(contentTmpResult);
    }

    /**
     *
     * <p>Title : findAll</p>
     * <p>Description : 查询ContentTmp全部数据</p>
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<ContentTmp>> findAll() {
        // 调用ContentTmpService实现查询所有ContentTmp
        List<ContentTmp> list = contentTmpService.list();
        return Result.ok(list);
    }

    /**
     * findHome
     * @param id
     * @return
     */
    @GetMapping(value = "/findHome")
    public Result<List<ContentTmp>> findHome() {
        // 调用UserService实现查询所有User
        QueryWrapper<ContentTmp> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("user_id");
        List<ContentTmp> list = contentTmpService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 清空临时表
     * @return
     */
    @GetMapping(value = "/truncate")
    public Result<Boolean> truncate() {
        return Result.ok(contentTmpService.truncate());
    }
}
